package com.java.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.java.model.*;
import com.java.service.*;
import com.java.utils.CommonUtils;
import com.java.utils.JsonBody;
import com.java.utils.ResponseUtil;
import com.sun.corba.se.spi.protocol.RequestDispatcherRegistry;;

@RestController
public class LoginController {
	@Autowired
	private AdminService adminService;
	@Autowired
	private ClientService clientService;

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;



	@CrossOrigin(origins = "*")
	@ResponseBody
	@RequestMapping("login")
	public JsonBody  Login(@RequestBody Login login) {
		if(login.getAuthor().equals("admin"))
		{
			Admin a = adminService.Login(login.getLogin());
			if(a==null)
				return Util.SetMap("用户不存在",false);
			else if(!a.getPwd().equals(login.getPwd()))
				return Util.SetMap("密码错误",false);
			else
			{
				JsonBody jb=new JsonBody();
				a.setAuthor("admin");
				jb.setData(a);
				jb.setToken(a.getId().toString());
				jb.setMsg("验证通过");
				return jb;
			}
		}
		else
		{
			Client a = clientService.Login(login.getLogin());
			if(a==null)
				return Util.SetMap("用户不存在",false);
			else if(!a.getPwd().equals(login.getPwd()))
				return Util.SetMap("密码错误",false);
			else
			{
				JsonBody jb=new JsonBody();
				a.setAuthor("client");
				jb.setData(a);
				jb.setToken(a.getId().toString());
				jb.setMsg("验证通过");
				return jb;
			}
		}
	}


}
